<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>邮件配置</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all">
    <link rel="stylesheet" th:href="@{/statics/wang/wangeditor.css}">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">邮件配置</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="layuiadmin-form-smtp">
                <div class="layui-form-item" th:each="config:${configs}">
                    <label class="layui-form-label" style="width: 150px" th:text="${config.title}">参数名称</label>
                    <div class="layui-input-block" style="margin-left: 180px;" th:if="${config.type} eq 1">
                        <input type="checkbox" th:checked="${config.value eq '1'}" th:name="${config.code}" lay-skin="switch"/>
                    </div>
                    <div class="layui-input-block" style="margin-left: 180px;" th:if="${config.type} eq 2">
                        <input type="text" th:name="${config.code}" autocomplete="off" th:placeholder="${config.description}"
                               class="layui-input" th:value="${config.value}">
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" id="LAY-smtp-form-submit" lay-filter="LAY-smtp-form-submit">立即提交
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        <button class="layui-btn layui-btn-normal" lay-submit="" id="LAY-send-test-mail" lay-filter="LAY-send-test-mail">发送测试邮件
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script>
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
        var $ = layui.$
            , admin = layui.admin
            , element = layui.element
            , layer = layui.layer
            , form = layui.form;

        form.render(null, 'layuiadmin-form-smtp');


        /* 监听提交 */
        form.on('submit(LAY-smtp-form-submit)', function (data) {
            if(data.field.email_smtp_ssl == "on") {
                data.field.email_smtp_ssl = "1";
            } else {
                data.field.email_smtp_ssl = "0";
            }
            $.ajax({
                url: '/api/config/save',
                method: 'post',
                data: data.field,
                dataType: 'JSON',
                async: true,
                success: function (result) {
                    if (result.code == '0') {
                        layer.msg('保存成功!', {icon: 1, time: 1000});
                    } else {
                        layer.msg('保存失败！' + result.msg, {icon: 2, time: 1000});
                    }
                },
                error: function (err) {
                    layer.msg('保存失败！', {icon: 2, time: 1000});
                }
            });
            return false;
        });
        form.on('submit(LAY-send-test-mail)', function (data) {
            if(data.field.email_smtp_ssl == "on") {
                data.field.email_smtp_ssl = "1";
            } else {
                data.field.email_smtp_ssl = "0";
            }
            $.ajax({
                url: '/api/mail/sendTest',
                method: 'post',
                data: data.field,
                dataType: 'JSON',
                async: true,
                success: function (result) {
                    if (result.code == '0') {
                        layer.msg('测试邮件发送成功,!', {icon: 1, time: 1000});
                    } else {
                        layer.msg('测试邮件发送失败！' + result.msg, {icon: 2, time: 1000});
                    }
                },
                error: function (err) {
                    layer.msg('保存失败！', {icon: 2, time: 1000});
                }
            });
            return false;
        });
    });
</script>

</body>
</html>
